package com.jingmi.jingmiliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.jingmi.jingmiliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.jingmi.jingmiliving.commodity.service.AttrAttrgroupRelationService;
import com.jingmi.jingmiliving.commodity.service.CategoryService;
import com.jingmi.jingmiliving.commodity.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jingmi.jingmiliving.commodity.entity.AttrgroupEntity;
import com.jingmi.jingmiliving.commodity.service.AttrgroupService;
import com.jingmi.common.utils.PageUtils;
import com.jingmi.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author jingmi
 * @email 2771368705@qq.com
 * @date 2023-10-18 14:36:33
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {
    @Autowired
    private AttrgroupService attrgroupService;
    @Autowired
    private CategoryService categoryService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @RequestMapping("/{catalogId}/withattr")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R addGroupAndBaseAttr(@PathVariable("catalogId") Long catalogId){

        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrgroupService.addAttrGroupWithAttrsVo(catalogId);
        return R.ok().put("data",attrGroupWithAttrsVos);
    }

    //commodity/attrgroup/attr/relation
    @RequestMapping("/attr/relation")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R addBatch(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntity){
    attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntity);
    return R.ok();
    }

    @RequestMapping("/{attrGroupId}/noattr/relation")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R ListbaseAttrOptional(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils pageUtils = attrgroupService.queryBaseAttrOptional(params, attrGroupId);
        return R.ok().put("page", pageUtils);
    }



    //在attrgroup查询baseattr
    @RequestMapping("/{attrGroupId}/attr/relation")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R ListbaseAttr(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        System.out.println("已进入");
        System.out.println("params==>"+params);
        Integer o = (Integer)params.get("list");
        System.out.println("o===>"+o);
        PageUtils pageUtils = attrgroupService.queryByAttrgroupId(params, attrGroupId);
        System.out.println("pageUtils==>"+pageUtils);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrgroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R listcondition(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId){
        PageUtils page = attrgroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrgroupEntity attrgroup = attrgroupService.getById(id);
        Long categoryId = attrgroup.getCategoryId();
        Long[] longs = categoryService.getcascaderCategoryIds(categoryId);
        attrgroup.setCascaderCategoryIds(longs);
        return R.ok().put("attrgroup", attrgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    //删除主分组下的属性/commodity/attrgroup/attr/relation/delete

    @RequestMapping("/attr/relation/delete")
    //@RequiresPermissions("commodity:attrgroup:delete")
    public R deleteBatchesAttr(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities){
        attrgroupService.deleteBatches(attrAttrgroupRelationEntities);
        return R.ok();
    }
}
